<template>
    <!-- 项目选择 -->

    <a-select :mode="mode" v-bind="$attrs" v-on="$listeners" :placeholder="placeholder" v-model="currentValue">
        <a-icon v-if="fetching" style="color: #096dd9" slot="suffixIcon" type="loading" />
        <a-spin v-if="fetching" slot="notFoundContent" size="small" />
        <a-select-option v-for="i in list" :key="i.id" :value="i.id">{{ i.name }}</a-select-option>
    </a-select>
</template>
<script>
import { getAllBranchCompany, getProjectIdFromOrg } from '@/api/commons'
export default {
    name: 'SelectUnit',
    components: {},
    props: {
        // v-model绑定的数据
        value: {
            type: [Array, String, Object, Number]
        },
        // 需要联动的表单名，
        form: {
            type: Object,
            default: () => ({})
        },
        // placeholder
        placeholder: {
            type: String,
            default: () => '请选择'
        },
        // 需要联动的表单名
        keyValue: {
            type: String,
            Array,
            default: () => 'unionLevelId'
        },
        orgIds: {
            type: Array,
            default: () => []
        },
        // 联动表单值改变时是否清空当前表单值
        isClear: {
            type: Boolean,
            default: () => true
        },
        isEdit: {
            type: Boolean,
            default: () => false
        },
        mode: {
            type: String,
            default: 'multiple'
        },
        // 是否使用所有层级参数
        useAll: {
            type: Boolean,
            default: false
        },
        //是否联动
        linkage: {
            type: Boolean,
            default: false
        },
        //系统为公司/分公司时，是否获取对应公司的下拉数据
        isCorrCompany: {
            type: Boolean,
            default: false
        }
    },
    mounted() {
        if (this.linkage) {
            this.getList()
        }
    },
    data() {
        return {
            fetching: false,
            isCanClear: false,
            list: []
        }
    },
    methods: {
        handelChage() {
            // if(this.orgLevel === 'PD'){
            //   return
            // }
            // // 重置列表
            this.list = []
            //  this.$set(this.form, 'unionChairmanId', '')
            // 单选的情况下，会清空值（避免影响组件之前的逻辑）
            if (this.isClear && this.mode === 'default') {
                this.currentValue = void 0
            }
            this.attr && this.getList()
        },
        getList() {
            this.fetching = true
            if (this.form[this.keyValue] != '605004528177254447' || this.useAll) {
                let params = { tagCodes: ['CO'] }
                if (this.isCorrCompany) {
                    let currentPost = JSON.parse(sessionStorage.getItem('ycloud-user_info')).currentPost;
                    if (currentPost.code === 'BC') {
                        params.tagCodes = ['BC']
                    } else if (currentPost.code === 'CO') {
                        params.tagCodes = ['BC', 'CO']
                    }
                } else {
                    if (this.linkage) {
                        params.tagCodes = ['BC', 'CO']
                    } else {
                        if (this.form[this.keyValue] == '605004340528287745') {
                            params.tagCodes = ['CO']
                        } else if (this.form[this.keyValue] == '605004401341501446') {
                            params.tagCodes = ['BC']
                        } else if (this.form[this.keyValue] == '605004528177254447') {
                            params.tagCodes = ['PD']
                        }
                    }
                }
                getAllBranchCompany(params).then((res) => {
                    this.fetching = false
                    this.list = res.data
                    if (params.tagCodes[0] == 'CO') {
                        this.currentValue = this.list[0].id
                        this.$emit('changeDepId', this.mode == 'multiple' ? [this.currentValue] : this.currentValue)
                    }

                    // console.log(params.tagCodes == 'CO')
                })
            } else {
                if (this.orgIds.length == 0) {
                    this.list = []
                    this.$set(this.form, 'unionChairmanId', '')
                    this.fetching = false
                    return
                }
                let params = {
                    pageNum: 1,
                    pageSize: 300,
                    pids: this.orgIds.join(','),
                    type: 3 //根据分公司orgId查询下面的项目部，过滤掉部门
                }
                getProjectIdFromOrg(params).then((res) => {
                    this.fetching = false

                    this.list = res.data.list
                })
            }
        },
        /**
         * 获取指定单位的name
         */
        getValueName(id) {
            const target = this.list.find((item) => item.id === id)
            return (target && target.name) || void 0
        }
    },

    computed: {
        // 需要联动的表单值
        attr() {
            return this.form[this.keyValue]
        },
        // 转换绑定的选中数据，与v-model同步
        currentValue: {
            get: function () {
                return this.value
            },
            set: function (value) {
                this.$emit('input', value)
                this.$emit('changeDepId', value)
                if (this.mode === 'default') {
                    this.$emit('changeName', this.getValueName(value))
                }
            }
        }
    },
    watch: {
        // 监听联动表单值重置项目列表
        //2020/12/21备注，在model里面使用，不好使就整个v-if
        /*
    *   v-if="visible"
        placeholder="请选择"
        v-model="queryParam.projectId"
        :form="queryParam"
        :isClear="false"
        :isEdit="true"
        keyValue="branchCompanyId"
    *
    * */
        attr() {
            this.handelChage()
        },
        orgIds: {
            deep: true,
            handler: function () {
                // 清空表单
                if (this.isClear && !this.isEdit) {
                    this.currentValue = void 0
                    this.$emit('resetSelf') // 在搜索组件(SearchData)中重置自身的值
                }
                // 清空表单
                if (this.isEdit && this.isCanClear) {
                    this.currentValue = void 0
                    this.$emit('resetSelf') // 在搜索组件(SearchData)中重置自身的值
                } else {
                    // this.isCanClear = true
                }

                this.handelChage()
            }
        }
    }
}
</script>
